package com.itheima.job;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @author LiangTianQi
 * @describe 定时清除无用图片
 * @date 2020/11/11 20:01
 */
@Component
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        //获取两个集合中的差值
        Jedis jedis = jedisPool.getResource();
        Set<String> sdiff = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_RESOURCES_DB);

        //对拿到的差集进行循环
        for (String imgName : sdiff) {
            //删除redis中保存在七牛云名称的set的图片名称
            jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES, imgName);
            //删除七牛云存储的图片
            QiniuUtils.deleteFileFromQiniu(imgName);

            System.out.println("删除无用的图片:"+imgName);
        }
    }
}
